﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Power.Model;
using Power.DAL;

namespace Power
{
    /// <summary>
    /// NewCus.xaml 的交互逻辑
    /// </summary>
    public partial class NewCus : Window
    {
        public NewCus()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {





            bool isOK = true;






            CheckTextboxNotEmpty(ref isOK, CusBox, CidBox, DeviceBox, AddBox, PhoneBox);
            if (!isOK)
            {
                MessageBox.Show("有未填写信息");
                return;
            }

            bool Chargeright = new CustomerDAL().JudgeByDevice(DeviceBox.Text);
            if (Chargeright)
            {
                MessageBox.Show("设备号重复");
                return;
            }

            Customer newcus = new Customer();
            newcus.CusName = CusBox.Text;
            newcus.CID = CidBox.Text;
            newcus.Device = DeviceBox.Text;
            newcus.Address = AddBox.Text;
            newcus.Phone = PhoneBox.Text;
            newcus.Charger = ChargerBox.Text;
            newcus.IsDeleted = false;
            new CustomerDAL().InsertnUser(newcus);
            MessageBox.Show("添加成功");
            DialogResult = false;


        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }




        private void CheckTextboxNotEmpty(ref bool isOK,
params TextBox[] textboxes)
        {
            foreach (TextBox txtBox in textboxes)
            {
                if (txtBox.Text.Length <= 0)
                {
                    isOK = false;
                    txtBox.Background = Brushes.Red;
                }
                else
                {
                    txtBox.Background = null;
                }
            }
        }






    }
}
